import "babel-polyfill";
import { Tool} from '../Tool';
import Util from "../utils/Util";
/**
 * 存储登录的用户信息
 *
 * @param {string} [state=JSON.parse(Tool.localItem('User'))]
 * @param {Object} action
 * @returns Object
 */
const User = (state = JSON.parse(Tool.localItem('User')), action) => {

    switch (action.type) {
        case 'loginSuccess': //登录成功
            Tool.localItem("company",action.UserJson.org_name);
            Tool.localItem('User', JSON.stringify(action.UserJson));
            return action.UserJson;
        case 'loginOut': //退出
            Util.loginOut();
            return null;
        default:
            return state;
    }
}

/* 登录组件（弹层） */
const Expert=(state={
    list:[],
    info:{
        expert:{},
        org:{},
        lab:{}
    },
    form:{
        visible:false,
        loading:false,
    }
},action)=>{
    switch (action.type){
        case 'set_expert_list':
            return Object.assign({}, state, {
                list: action.result
            });
        case "set_visible":
            var form = state["form"];
            form.visible = action.visible;
            return Object.assign({}, state, {
                form: form
            });
        case "set_expert_info_uid":
            var list = state["list"].content,info=state["info"];
            if(!Util.IsNull(action.uid)){
                list.map(function(item){
                    if(item.uid == action.uid){
                        info.expert = item;
                    }
                })
            }else{
                info.expert={}
            }
            return Object.assign({}, state, {
                info: info
            });
        case "set_expert_lab":
            var info = state["info"];
            info.lab=action.result;
            return Object.assign({}, state, {
                info: info
            });
        case "set_expert_org":
            var info = state["info"];
            info.org=action.result;
            return Object.assign({}, state, {
                info: info
            });
        case "set_expert_info_update":
            var list = state["list"];
            var newList = [];
            list.content.map(function(item){
                if(item.uid == action.values.uid){
                    item.name = action.values.name;
                    item.email = action.values.email;
                    item.resume = action.values.resume;
                    item.honors = action.values.honors;
                    item.hindex = action.values.hindex;
                    item.domain = action.values.domain;
                    item.mobile = action.values.mobile;
                    item.office_phone = action.values.phone;
                    item.lab = action.values.lab;
                    item.orgList = action.values.orgList;
                    item.title = action.values.title;
                    console.log(item)
                }
                newList.push(item);
            })
            list.content = newList;
            return Object.assign({}, state, {
                list: list
            });
        default:
            return state;
    }
};

const CommonSystem=(state={
    loading:false,
    searchLoading:false,
    listLoading:false
},action)=>{
    switch (action.type){
        case 'set_common_loading':
            return Object.assign({}, state, {
                loading: action.loading
            });
        case 'set_common_search_loading':
            return Object.assign({}, state, {
                searchLoading: action.loading
            });
        case 'set_common_list_loading':
            return Object.assign({}, state, {
                listLoading: action.loading
            });
        default:
            return state;
    }
};

export default {
    User,Expert,CommonSystem
};